<template>
  <div>
    <FormPanel ref="formPanel" :list="formList" @formChange="formChangeHandle" :isSubmitBtn="false" :collapse="false" labelWidth="130" />

    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'center'
      }"
    >
      <multiuse-button type="primary" :click="saveDataHandle.bind(this, 60841003)">确 认</multiuse-button>
      <multiuse-button type="primary" :click="saveDataHandle.bind(this, 60841002)">保 存</multiuse-button>
      <el-button @click="closeDrawer">关 闭</el-button>
    </div>
  </div>
</template>
<script>
import moment from 'moment';
import { dictionary } from '@/utils/dictMixin';
import { queryById, saveData } from '@/api/dealerFinance/RebateManage/RebateStatementCommit';

export default {
  mixins: [dictionary],
  props: ['rowData'],

  data() {
    this.saveData = null;
    return {
      formList: this.createdFrom()
    };
  },
  created() {
    this.init();
  },
  methods: {
    async init() {
      const res = await queryById(this.rowData.id);
      if (res.resultCode == 200) {
        this.formList.map(x => {
          x.initialValue = res.data[x.fieldName];
        });
      }
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    async saveDataHandle(status) {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData) {
        this.saveData.id = this.rowData.id;
        // 已驳回单据，还是已驳回；
        this.saveData.status = status == 60841003 ? status : this.rowData.status == 60841004 ? 60841004 : status;
        let res = await saveData(this.saveData);

        if (res.resultCode == 200) {
          this.$notify.success({ title: '提示信息', message: '操作成功' });
          this.$emit('close', false, 'refresh');
        }
      }
    },
    closeDrawer() {
      this.$emit('close');
    },

    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '对账单号',
          fieldName: 'verifyNo',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '账单名称',
          fieldName: 'verifyName',
          placeholder: '',
          disabled: true
        },

        {
          type: 'SELECT',
          label: '对账分类',
          fieldName: 'verifyType',
          placeholder: '请选择',
          itemList: this.createDictList('6086'),
          disabled: true
        },
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '',
          disabled: true
        },
        {
          type: 'DATE',
          label: '生成日期',
          fieldName: 'createdAt',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '整车款余额',
          fieldName: 'vehicleAccountAmount',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '差异金额',
          fieldName: 'vehicleAccountDifferences',
          placeholder: '',
          onInput: val => {
            if (val) {
              console.log(val, '差异金额');
              let adjust = 0;
              let balance = this.formList.find(x => x.fieldName == 'vehicleAccountAmount').initialValue;
              adjust = parseFloat(balance) + parseFloat(val);
              console.log(adjust, '调整后金额');
              this.formList.find(x => x.fieldName == 'vehicleAccountAdjustment').initialValue = adjust || 0;
              return;
            }
            this.formList.find(x => x.fieldName === 'vehicleAccountAdjustment').initialValue = '';
          }
        },
        {
          type: 'INPUT',
          label: '调整后金额',
          fieldName: 'vehicleAccountAdjustment',
          initialValue: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '整车返利余额',
          fieldName: 'vehicleRebateAmount',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '差异金额',
          fieldName: 'vehicleRebateDifferences',
          placeholder: '',
          onInput: val => {
            if (val) {
              console.log(val, '差异金额');
              let adjust = 0;
              let balance = this.formList.find(x => x.fieldName == 'vehicleRebateAmount').initialValue;
              adjust = parseFloat(balance) + parseFloat(val);
              console.log(adjust, '调整后金额');
              this.formList.find(x => x.fieldName == 'vehicleRebateAdjustment').initialValue = adjust || 0;
              return;
            }
            this.formList.find(x => x.fieldName === 'vehicleRebateAdjustment').initialValue = '';
          }
        },
        {
          type: 'INPUT',
          label: '调整后金额',
          fieldName: 'vehicleRebateAdjustment',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '备件款余额',
          fieldName: 'partAccountAmount',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '差异金额',
          fieldName: 'partAccountDifferences',
          placeholder: '',
          onInput: val => {
            if (val) {
              console.log(val, '差异金额');
              let adjust = 0;
              let balance = this.formList.find(x => x.fieldName == 'partAccountAmount').initialValue;
              adjust = parseFloat(balance) + parseFloat(val);
              console.log(adjust, '调整后金额');
              this.formList.find(x => x.fieldName == 'partAccountAdjustment').initialValue = adjust || 0;
              return;
            }
            this.formList.find(x => x.fieldName === 'partAccountAdjustment').initialValue = '';
          }
        },
        {
          type: 'INPUT',
          label: '调整后金额',
          fieldName: 'partAccountAdjustment',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '备件返利余额',
          fieldName: 'partRebateAmount',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '差异金额',
          fieldName: 'partRebateDifferences',
          placeholder: '',
          onInput: val => {
            if (val) {
              console.log(val, '差异金额');
              let adjust = 0;
              let balance = this.formList.find(x => x.fieldName == 'partRebateAmount').initialValue;
              adjust = parseFloat(balance) + parseFloat(val);
              console.log(adjust, '调整后金额');
              this.formList.find(x => x.fieldName == 'partRebateAdjustment').initialValue = adjust || 0;
              return;
            }
            this.formList.find(x => x.fieldName === 'partRebateAdjustment').initialValue = '';
          }
        },
        {
          type: 'INPUT',
          label: '调整后金额',
          fieldName: 'partRebateAdjustment',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '建店保证金余额',
          fieldName: 'depositAmount',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '差异金额',
          fieldName: 'depositDifferences',
          placeholder: '',
          onInput: val => {
            if (val) {
              console.log(val, '差异金额');
              let adjust = 0;
              let balance = this.formList.find(x => x.fieldName == 'depositAmount').initialValue;
              adjust = parseFloat(balance) + parseFloat(val);
              console.log(adjust, '调整后金额');
              this.formList.find(x => x.fieldName == 'depositAdjustment').initialValue = adjust || 0;
              return;
            }
            this.formList.find(x => x.fieldName === 'depositAdjustment').initialValue = '';
          }
        },
        {
          type: 'INPUT',
          label: '调整后金额',
          fieldName: 'depositAdjustment',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '其他类应收款项',
          fieldName: 'otherInAmount',
          placeholder: '',
          disabled: true
        },

        {
          type: 'INPUT',
          label: '差异金额',
          fieldName: 'otherInDifferences',
          placeholder: '',
          onInput: val => {
            if (val) {
              console.log(val, '差异金额');
              let adjust = 0;
              let balance = this.formList.find(x => x.fieldName == 'otherInAmount').initialValue;
              adjust = parseFloat(balance) + parseFloat(val);
              console.log(adjust, '调整后金额');
              this.formList.find(x => x.fieldName == 'otherInAdjustment').initialValue = adjust || 0;
              return;
            }
            this.formList.find(x => x.fieldName === 'otherInAdjustment').initialValue = '';
          }
        },
        {
          type: 'INPUT',
          label: '调整后金额',
          fieldName: 'otherInAdjustment',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '其他类应付款项',
          fieldName: 'otherOutAmount',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '差异金额',
          fieldName: 'otherOutDifferences',
          placeholder: '',
          onInput: val => {
            if (val) {
              console.log(val, '差异金额');
              let adjust = 0;
              let balance = this.formList.find(x => x.fieldName == 'otherOutAmount').initialValue;
              adjust = parseFloat(balance) + parseFloat(val);
              console.log(adjust, '调整后金额');
              this.formList.find(x => x.fieldName == 'otherOutAdjustment').initialValue = adjust || 0;
              return;
            }
            this.formList.find(x => x.fieldName === 'otherOutAdjustment').initialValue = '';
          }
        },
        {
          type: 'INPUT',
          label: '调整后金额',
          fieldName: 'otherOutAdjustment',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '账户资金所属公司',
          fieldName: 'orgCompanyName',
          placeholder: '',
          disabled: true
        },
        {
          type: 'SELECT',
          label: '对账结果',
          fieldName: 'verifyResult',
          placeholder: '请选择',
          itemList: this.createDictList('6085'),
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '经销商反馈差异金额',
          fieldName: 'feedbackAmountDesc',
          placeholder: '',
          selfCols: 3,
          rules: [{ required: true, message: '请输入', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '经销商反馈差异说明',
          fieldName: 'feedbackExplainDesc',
          placeholder: '',
          selfCols: 3,
          rules: [{ required: true, message: '请输入', trigger: 'blur' }]
        }
      ];
    }
  }
};
</script>
